{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score, confusion_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read in the data from the CSV file\n",
    "df = pd.read_csv('datasets/payment_fraud.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>accountAgeDays</th>\n",
       "      <th>numItems</th>\n",
       "      <th>localTime</th>\n",
       "      <th>paymentMethod</th>\n",
       "      <th>paymentMethodAgeDays</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12221</th>\n",
       "      <td>1045</td>\n",
       "      <td>1</td>\n",
       "      <td>4.505662</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36961</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3.575983</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12589</th>\n",
       "      <td>1961</td>\n",
       "      <td>1</td>\n",
       "      <td>4.921349</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>186.035417</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2526</th>\n",
       "      <td>579</td>\n",
       "      <td>1</td>\n",
       "      <td>4.895263</td>\n",
       "      <td>paypal</td>\n",
       "      <td>34.721528</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20964</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>4.962055</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>128.497222</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11717</th>\n",
       "      <td>1976</td>\n",
       "      <td>1</td>\n",
       "      <td>4.745402</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13600</th>\n",
       "      <td>1644</td>\n",
       "      <td>1</td>\n",
       "      <td>4.962055</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8375</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>4.876771</td>\n",
       "      <td>paypal</td>\n",
       "      <td>375.588889</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29591</th>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>4.965339</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>44.139583</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34313</th>\n",
       "      <td>1209</td>\n",
       "      <td>2</td>\n",
       "      <td>4.524580</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>0.020833</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25712</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4.962055</td>\n",
       "      <td>paypal</td>\n",
       "      <td>0.001389</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37196</th>\n",
       "      <td>31</td>\n",
       "      <td>2</td>\n",
       "      <td>4.886641</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32667</th>\n",
       "      <td>512</td>\n",
       "      <td>1</td>\n",
       "      <td>4.524580</td>\n",
       "      <td>paypal</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36154</th>\n",
       "      <td>1587</td>\n",
       "      <td>1</td>\n",
       "      <td>5.034622</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>0.002083</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30017</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4.836982</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>0.008333</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1105</th>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "      <td>4.524580</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>25.702083</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4459</th>\n",
       "      <td>514</td>\n",
       "      <td>1</td>\n",
       "      <td>4.057414</td>\n",
       "      <td>paypal</td>\n",
       "      <td>1.040278</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29969</th>\n",
       "      <td>1097</td>\n",
       "      <td>1</td>\n",
       "      <td>4.965339</td>\n",
       "      <td>paypal</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25584</th>\n",
       "      <td>1526</td>\n",
       "      <td>1</td>\n",
       "      <td>4.057414</td>\n",
       "      <td>paypal</td>\n",
       "      <td>174.113889</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16879</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>5.017904</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29092</th>\n",
       "      <td>178</td>\n",
       "      <td>1</td>\n",
       "      <td>3.483951</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>97.879167</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4479</th>\n",
       "      <td>926</td>\n",
       "      <td>1</td>\n",
       "      <td>4.505662</td>\n",
       "      <td>paypal</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38638</th>\n",
       "      <td>1268</td>\n",
       "      <td>1</td>\n",
       "      <td>4.876771</td>\n",
       "      <td>paypal</td>\n",
       "      <td>0.340972</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22678</th>\n",
       "      <td>25</td>\n",
       "      <td>1</td>\n",
       "      <td>4.921349</td>\n",
       "      <td>paypal</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18864</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>4.876771</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>1163.151389</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9340</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>4.505662</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>0.002778</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27379</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>4.965339</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>260.971528</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14383</th>\n",
       "      <td>2000</td>\n",
       "      <td>2</td>\n",
       "      <td>4.524580</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22274</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>4.921318</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4071</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4.836982</td>\n",
       "      <td>creditcard</td>\n",
       "      <td>1.967361</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       accountAgeDays  numItems  localTime paymentMethod  \\\n",
       "12221            1045         1   4.505662    creditcard   \n",
       "36961               1         2   3.575983    creditcard   \n",
       "12589            1961         1   4.921349    creditcard   \n",
       "2526              579         1   4.895263        paypal   \n",
       "20964            2000         1   4.962055    creditcard   \n",
       "11717            1976         1   4.745402    creditcard   \n",
       "13600            1644         1   4.962055    creditcard   \n",
       "8375             2000         1   4.876771        paypal   \n",
       "29591              45         1   4.965339    creditcard   \n",
       "34313            1209         2   4.524580    creditcard   \n",
       "25712               2         1   4.962055        paypal   \n",
       "37196              31         2   4.886641    creditcard   \n",
       "32667             512         1   4.524580        paypal   \n",
       "36154            1587         1   5.034622    creditcard   \n",
       "30017               2         1   4.836982    creditcard   \n",
       "1105               26         1   4.524580    creditcard   \n",
       "4459              514         1   4.057414        paypal   \n",
       "29969            1097         1   4.965339        paypal   \n",
       "25584            1526         1   4.057414        paypal   \n",
       "16879            2000         1   5.017904    creditcard   \n",
       "29092             178         1   3.483951    creditcard   \n",
       "4479              926         1   4.505662        paypal   \n",
       "38638            1268         1   4.876771        paypal   \n",
       "22678              25         1   4.921349        paypal   \n",
       "18864            2000         1   4.876771    creditcard   \n",
       "9340                5         1   4.505662    creditcard   \n",
       "27379            2000         1   4.965339    creditcard   \n",
       "14383            2000         2   4.524580    creditcard   \n",
       "22274               4         1   4.921318    creditcard   \n",
       "4071                2         1   4.836982    creditcard   \n",
       "\n",
       "       paymentMethodAgeDays  label  \n",
       "12221              0.000000      0  \n",
       "36961              0.000000      1  \n",
       "12589            186.035417      0  \n",
       "2526              34.721528      0  \n",
       "20964            128.497222      0  \n",
       "11717              0.000000      0  \n",
       "13600              0.000000      0  \n",
       "8375             375.588889      0  \n",
       "29591             44.139583      0  \n",
       "34313              0.020833      0  \n",
       "25712              0.001389      0  \n",
       "37196              0.000000      0  \n",
       "32667              0.000000      0  \n",
       "36154              0.002083      0  \n",
       "30017              0.008333      0  \n",
       "1105              25.702083      0  \n",
       "4459               1.040278      0  \n",
       "29969              0.000000      0  \n",
       "25584            174.113889      0  \n",
       "16879              0.000000      0  \n",
       "29092             97.879167      0  \n",
       "4479               0.000000      0  \n",
       "38638              0.340972      0  \n",
       "22678              0.000000      0  \n",
       "18864           1163.151389      0  \n",
       "9340               0.002778      0  \n",
       "27379            260.971528      0  \n",
       "14383              0.000000      0  \n",
       "22274              0.000000      0  \n",
       "4071               1.967361      0  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sample(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>accountAgeDays</th>\n",
       "      <th>numItems</th>\n",
       "      <th>localTime</th>\n",
       "      <th>paymentMethodAgeDays</th>\n",
       "      <th>label</th>\n",
       "      <th>paymentMethod_creditcard</th>\n",
       "      <th>paymentMethod_paypal</th>\n",
       "      <th>paymentMethod_storecredit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>24205</th>\n",
       "      <td>2000</td>\n",
       "      <td>1</td>\n",
       "      <td>4.895263</td>\n",
       "      <td>753.620139</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21090</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4.962055</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36676</th>\n",
       "      <td>120</td>\n",
       "      <td>1</td>\n",
       "      <td>4.524580</td>\n",
       "      <td>119.510417</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       accountAgeDays  numItems  localTime  paymentMethodAgeDays  label  \\\n",
       "24205            2000         1   4.895263            753.620139      0   \n",
       "21090               3         1   4.962055              0.000000      0   \n",
       "36676             120         1   4.524580            119.510417      0   \n",
       "\n",
       "       paymentMethod_creditcard  paymentMethod_paypal  \\\n",
       "24205                         1                     0   \n",
       "21090                         1                     0   \n",
       "36676                         1                     0   \n",
       "\n",
       "       paymentMethod_storecredit  \n",
       "24205                          0  \n",
       "21090                          0  \n",
       "36676                          0  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Convert categorical feature into dummy variables with one-hot encoding\n",
    "df = pd.get_dummies(df, columns=['paymentMethod'])\n",
    "df.sample(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Split dataset up into train and test sets\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    df.drop('label', axis=1), df['label'],\n",
    "    test_size=0.33, random_state=17)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize and train classifier model\n",
    "clf = LogisticRegression().fit(X_train, y_train)\n",
    "\n",
    "# Make predictions on test set\n",
    "y_pred = clf.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.99992273816\n",
      "[[12753     0]\n",
      " [    1   189]]\n"
     ]
    }
   ],
   "source": [
    "# Compare test set predictions with ground truth labels\n",
    "print(accuracy_score(y_pred, y_test))\n",
    "print(confusion_matrix(y_test, y_pred))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
